package com.ccgk.logistics.service.impl;

import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.HashMap;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.ccgk.common.MyDateUtils;
import com.ccgk.common.ReportUtils;
import com.ccgk.logistics.domain.Order;
import com.ccgk.logistics.service.OrderService;
import com.ccgk.logistics.service.ReportService;

@Service
public class ReportServiceImpl implements ReportService{

	@Autowired
	private OrderService orderService;
	
	@Override
	public void exportHbl(Integer orderId,OutputStream out) {
		Order order = orderService.getById(orderId);
		Map<String,String> dataSource = new HashMap<String,String>();
		dataSource.put("shipperDetail", order.getShipperDetail());
		dataSource.put("orderCode", order.getOrderCode());
		dataSource.put("so", order.getSo());
		dataSource.put("consignee", order.getConsigneeDetail());
		dataSource.put("notifier", order.getNotifierDetail());
		dataSource.put("shippingNameNumber", order.getShippingNameNumber());
		dataSource.put("receiptPlace", order.getReceiptPlace()==null?null:order.getReceiptPlace().getName());
		dataSource.put("loadingPort", order.getLoadingPort()==null?null:order.getLoadingPort().getName());
		dataSource.put("unloadingPort", order.getUnloadingPort()==null?null:order.getUnloadingPort().getName());
		dataSource.put("destinationPlace", order.getDestinationPlace()==null?null:order.getDestinationPlace().getName());
		dataSource.put("sealingNumber", order.getSealingNumber());
		dataSource.put("shippingMark", order.getShippingMark());
		dataSource.put("englishName", order.getEnglishName());
		dataSource.put("grossWeight", order.getGrossWeight()==null?null:order.getGrossWeight().toString());
		dataSource.put("volume", order.getVolume()==null?null:order.getVolume().toString());
		dataSource.put("infoSummy", order.getInfoSummy());
		dataSource.put("hPaymentType", order.gethPaymentType()==null?null:order.gethPaymentType().equals(1)?"FREIGHT PREPAID":"FREIGHT COLLECT");
		dataSource.put("cargoCollectionDetail", order.getCargoCollectionDetail());
		dataSource.put("shippingDate", MyDateUtils.format(order.getShippingDate(), "d/MMM/yy"));
		dataSource.put("issueDate", MyDateUtils.format(order.getIssueDate(), "d/MMM/yy"));
		InputStream template = ReportServiceImpl.class.getResourceAsStream("/HBL.xls");
		try {
			ReportUtils.fillReport(template, out, dataSource);
		} catch (IOException e) {
			throw new RuntimeException(e);
		}finally{
			try {
				template.close();
			} catch (IOException e) {
			}
		}
	}

	
	
}
